package _case

import (
	"fmt"
	"sort"
)

type SortUser struct {
	ID   int
	Name string
	Age  int
}

type ById []SortUser

func (u ById) Len() int           { return len(u) }
func (u ById) Less(i, j int) bool { return u[i].ID < u[j].ID }
func (u ById) Swap(i, j int)      { u[i], u[j] = u[j], u[i] }

func SortCase() {
	list := []SortUser{
		{1, "Tom", 15},
		{2, "Lily", 32},
		{3, "Jack", 42},
		{4, "Jerry", 21},
		{5, "Ac", 31},
	}
	sort.Slice(list, func(i, j int) bool {
		return list[i].Age < list[j].Age
	})
	fmt.Println(list)

	//实现sort.interface{}接口
	sort.Sort(ById(list))
	fmt.Println(list)
}
